home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Time / NTP.pm next >
Encoding:
Perl POD Document  |  2009-04-09  |  4.4 KB  |  188 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3. # NTP Configuration handling
  4. #
  5. # Copyright (C) 2000-2001 Ximian, Inc.
  6. #
  7. # Authors: Hans Petter Jansson <hpj@ximian.com>
  8. #          Carlos Garnacho     <carlosg@gnome.org>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU Library General Public License as published
  12. # by the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. # GNU Library General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Library General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  23.  
  24. package Time::NTP;
  25.  
  26. sub get_config_file ()
  27. {
  28.   my %dist_map =
  29.   (
  30.     "redhat-6.2"      => "redhat-6.2",
  31.     "redhat-7.0"      => "redhat-6.2",
  32.     "redhat-7.1"      => "redhat-6.2",
  33.     "redhat-7.2"      => "redhat-6.2",
  34.     "redhat-7.3"      => "redhat-6.2",
  35.     "redhat-8.0"      => "redhat-6.2",
  36.     "mandrake-9.0"    => "redhat-6.2",
  37.     "debian-3.0"      => "redhat-6.2",
  38.     "debian-3.1"      => "redhat-6.2",
  39.     "debian-4.0"      => "redhat-6.2",
  40.     "debian-5.0"      => "redhat-6.2",
  41.     "debian-testing"  => "redhat-6.2",
  42.     "ubuntu-7.04"     => "redhat-6.2",
  43.     "suse-9.0"        => "redhat-6.2",
  44.     "slackware-9.1.0" => "redhat-6.2",
  45.     "gentoo"          => "redhat-6.2",
  46.     "pld-1.0"         => "pld-1.0",
  47.     "vine-3.0"        => "redhat-6.2",
  48.     "freebsd-5"       => "redhat-6.2",
  49.     "archlinux"       => "redhat-6.2",
  50.     "solaris-2.11"    => "solaris-2.11",
  51.   );
  52.  
  53.   my %dist_table =
  54.   (
  55.     "redhat-6.2"   => "/etc/ntp.conf",
  56.     "pld-1.0"      => "/etc/ntp/ntp.conf",
  57.     "solaris-2.11" => "/etc/inet/ntp.conf",
  58.   );
  59.  
  60.   my $dist = $dist_map{$Utils::Backend::tool{"platform"}};
  61.   return $dist_table{$dist} if $dist;
  62.  
  63.   &Utils::Report::do_report ("platform_no_table", $$tool{"platform"});
  64.   return undef;
  65. }
  66.  
  67. sub get_ntp_servers
  68. {
  69.   $ntp_conf = &get_config_file ();
  70.  
  71.   return &Utils::Parse::split_all_array_with_pos ($ntp_conf, "server", 0, "[ \t]+", "[ \t]+");
  72. }
  73.  
  74. sub ntp_conf_replace
  75. {
  76.   my ($file, $key, $re, $value) = @_;
  77.   my ($fd, @line, @res);
  78.   my ($buff, $i);
  79.   my ($pre_space, $post_comment);
  80.   my ($line_key, $val, $rest);
  81.   my ($n, $ret);
  82.  
  83.   &Utils::Report::enter ();
  84.   &Utils::Report::do_report ("replace_split", $key, $file);
  85.  
  86.   $buff = &Utils::File::load_buffer ($file);
  87.   
  88.   foreach $i (@$buff)
  89.   {
  90.     $pre_space = $post_comment = "";
  91.  
  92.     chomp $i;
  93.  
  94.     $pre_space    = $1 if $i =~ s/^([ \t]+)//;
  95.     $post_comment = $1 if $i =~ s/([ \t]*\#.*)//;
  96.     
  97.     if ($i ne "")
  98.     {
  99.       @line = split ($re, $i, 3);
  100.       $line_key = shift (@line);
  101.       $val      = shift (@line);
  102.       $rest     = shift (@line);
  103.  
  104.       # found the key?
  105.       if ($line_key eq $key)
  106.       {
  107.         $n = 0;
  108.  
  109.         while (@$value[$n] && (@$value[$n] ne $val))
  110.         {
  111.           $n++;
  112.         }
  113.  
  114.         if (@$value[$n] ne $val)
  115.         {
  116.           $i = "";
  117.           next;
  118.         }
  119.  
  120.         delete @$value[$n];
  121.         chomp $val;
  122.         $i  = &Utils::Replace::set_value ($key, $val, $re) . " " . $rest;
  123.       }
  124.     }
  125.  
  126.     $i = $pre_space . $i . $post_comment . "\n";
  127.   }
  128.  
  129.   foreach $i (@$value)
  130.   {
  131.     push (@$buff, &Utils::Replace::set_value ($key, $i, $re) . "\n") if ($i ne "");
  132.   }
  133.  
  134.   &Utils::File::clean_buffer ($buff);
  135.   $ret = &Utils::File::save_buffer ($buff, $file);
  136.   &Utils::Report::leave ();
  137.   return $ret;
  138. }
  139.  
  140. sub set_ntp_servers
  141. {
  142.   my (@config) = @_;
  143.   my ($ntp_conf);
  144.  
  145.   $ntp_conf = &get_config_file ();
  146.   return &ntp_conf_replace ($ntp_conf, "server", "[ \t]+", @config);
  147. }
  148.  
  149. sub apply_ntp_date
  150. {
  151.   my ($config) = @_;
  152.   my ($servers, $server);
  153.  
  154.   foreach $server (@$config) {
  155.       $servers .= " $server";
  156.   }
  157.  
  158.   if ($server eq "") {
  159.       # There are no servers, pick them from the ntp.org pool
  160.       $server = "0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org";
  161.   }
  162.  
  163.   # run ntpdate, this will only be effective
  164.   # when there isn't any NTP server running
  165.   &Utils::File::run ("ntpdate -b $servers") if ($servers);
  166. }
  167.  
  168. sub get
  169. {
  170.   return &get_ntp_servers ();
  171. }
  172.  
  173. sub set
  174. {
  175.   &apply_ntp_date (@_);
  176.   return &set_ntp_servers (@_);
  177. }
  178.  
  179. sub get_files
  180. {
  181.   my ($files);
  182.  
  183.   push @$files, &get_config_file ();
  184.   return $files;
  185. }
  186.  
  187. 1;
  188.